package com.androidx.p013lv.base.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import com.androidx.p013lv.base.BaseSdk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/* renamed from: com.androidx.lv.base.utils.CrashHandler */
public class CrashHandler implements Thread.UncaughtExceptionHandler {
    private static CrashHandler INSTANCE = new CrashHandler();
    public static final String TAG = "CrashHandler";
    private DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss");
    private Map<String, String> infos = new HashMap();
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    private String path = "";

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        return INSTANCE;
    }

    public void init() {
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public void uncaughtException(Thread thread, Throwable th) {
        Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
        if (handleException(th) || (uncaughtExceptionHandler = this.mDefaultHandler) == null) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                Log.e(TAG, "error : ", e);
            }
            Process.killProcess(Process.myPid());
            System.exit(1);
            return;
        }
        uncaughtExceptionHandler.uncaughtException(thread, th);
    }

    public boolean handleException(Throwable th) {
        if (th == null) {
            return false;
        }
        collectDeviceInfo();
        saveCrashInfo2File(th);
        new Thread() {
            /* class com.androidx.p013lv.base.utils.CrashHandler.C10501 */

            public void run() {
                Looper.prepare();
                Looper.loop();
            }
        }.start();
        return false;
    }

    public void collectDeviceInfo() {
        try {
            PackageInfo packageInfo = BaseSdk.getContext().getPackageManager().getPackageInfo(BaseSdk.getContext().getPackageName(), 1);
            if (packageInfo != null) {
                this.infos.put("versionName", packageInfo.versionName == null ? "null" : packageInfo.versionName);
                this.infos.put("versionCode", packageInfo.versionCode + "");
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "an error occured when collect package info", e);
        }
        Field[] declaredFields = Build.class.getDeclaredFields();
        for (Field field : declaredFields) {
            try {
                field.setAccessible(true);
                this.infos.put(field.getName(), field.get(null).toString());
            } catch (Exception e2) {
                Log.e(TAG, "an error occured when collect crash info", e2);
            }
        }
    }

    public String saveCrashInfo2File(Throwable th) {
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, String> entry : this.infos.entrySet()) {
            stringBuffer.append(entry.getKey() + "=" + entry.getValue() + "\n");
        }
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        th.printStackTrace(printWriter);
        for (Throwable cause = th.getCause(); cause != null; cause = cause.getCause()) {
            cause.printStackTrace(printWriter);
        }
        printWriter.close();
        stringBuffer.append(stringWriter.toString());
        try {
            long currentTimeMillis = System.currentTimeMillis();
            String str = "crash-" + this.formatter.format(new Date()) + "-" + currentTimeMillis + ".txt";
            if (Environment.getExternalStorageState().equals("mounted")) {
                String logPath = getLogPath();
                File file = new File(logPath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(logPath + str);
                fileOutputStream.write(stringBuffer.toString().getBytes());
                fileOutputStream.close();
            }
            return str;
        } catch (Exception e) {
            Log.e(TAG, "an error occured while writing file...", e);
            return null;
        }
    }

    public void setLogPath(String str) {
        if (!TextUtils.isEmpty(str)) {
            this.path = str;
        }
    }

    public String getLogPath() {
        if (!TextUtils.isEmpty(this.path)) {
            return this.path;
        }
        return BaseSdk.getContext().getExternalFilesDir(null).getAbsolutePath() + "/crashinfo/";
    }
}
